package com.fire.service.impl;

import com.fire.dao.IStudentDao;
import com.fire.domain.Student;
import com.fire.factory.Factory;
import com.fire.service.IStudentService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class StudentServiceImpl implements IStudentService {
    private IStudentDao studentDao = Factory.getStudentDaoImpl();

    @Override
    public void add(Student student) {
        studentDao.add(student);
    }

    @Override
    public void delete(String id) throws IOException {
        studentDao.delete(id);
    }

    @Override
    public void update(String id, Student student) throws IOException {
        studentDao.update(id, student);
    }

    @Override
    public ArrayList<Student> findAll() {
        return studentDao.findAll();
    }

    @Override
    public boolean isExist(String id) {
        ArrayList<Student> list = studentDao.findAll();
        if (list == null || list.size() == 0) {
            return false;
        }
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getId().equals(id)) {
                return true;         //找到了
            }
        }
        return false;

    }

    @Override
    public ArrayList<Integer> getfind() throws IOException {
        return studentDao.getfind();
    }

    @Override
    public HashMap<Integer, ArrayList<Student>> getGroup() throws IOException {
        return studentDao.getGroup();
    }

    @Override
    public void daochu() {
        studentDao.daochu();
    }

    @Override
    public void daoru() {
        studentDao.daoru();
    }

    @Override
    public ArrayList<Student> searchName(String name) {
        ArrayList<Student> all = findAll();
        ArrayList<Student> students = new ArrayList<>();
        for (Student student : all) {
            if (name.equals(student.getName())) {
                students.add(student);
            }
        }
        return students;
    }

    @Override
    public ArrayList<Student> searchPhone(String phone) {
        ArrayList<Student> all = findAll();
        ArrayList<Student> students = new ArrayList<>();
        for (Student student : all) {
            if (phone.equals(student.getPhoneNum())) {
                students.add(student);
            }
        }
        return students;
    }

}

